package com.example.common.tools

import java.math.BigDecimal
import java.util.regex.Pattern

fun String?.isEmpty(): Boolean {
    var result = false
    if (this == null || "" == this.trim { it <= ' ' }) {
        result = true
    }
    return result
}

fun String?.isNotEmpty():Boolean{
    return !this.isEmpty()
}

fun String?.value():String{
    return this ?: ""
}

object StringUtil {

    fun isEmpty(key: String?): Boolean {
        var result = false
        if (key == null || "" == key.trim { it <= ' ' }) {
            result = true
        }
        return result
    }

    /**
     * 判断字符串非空
     *
     * @param str
     * @return
     */
    fun isNotEmpty(str: String?): Boolean {
        return str != null && str.trim().isNotEmpty()
    }

    fun isEmptyNormal(key: String?): Boolean {
        var result = false
        if (key == null || "" == key.trim { it <= ' ' }) {
            result = true
        }
        return result
    }

    fun isFileUrl(url: String): Boolean {
        var result = false
        if (!isEmpty(url)) {
            val f = url.substring(0, 1)
            if ("/" == f) {
                result = true
            }
        }
        return result
    }

    fun noNull(str: String?): String {
        return str ?: ""

    }

    fun strEquals(str1: String?, str2: String?): Boolean {
        return if (isEmpty(str1) || isEmpty(str2)) {
            false
        } else {
            str1 == str2
        }
    }

    fun checkPhoneNum(phoneNum: String?): Boolean {
        return isNotEmpty(phoneNum) && phoneNum!!.length == 11
    }

    //四舍五入取整数
    fun getIntegral(obj: Any): String {
        return if (obj is Double || obj is Float) {
            Math.round(obj as Double).toString() + ""
        } else if (obj is Int || obj is Long) {
            obj.toString() + ""
        } else {
            obj.toString() + ""
        }
    }

    //现金 四舍五入 保留2位小数
    fun getDecimal(one: Double, two: Double): String {
        val b1 = BigDecimal.valueOf(one)
        val b2 = BigDecimal.valueOf(two)
        val decimal = b1.multiply(b2)
        val money = decimal.setScale(2, BigDecimal.ROUND_HALF_UP).toString()
        return if (money == "0.00") "0" else money
    }

    /**
     * 匹配手机号的规则：[3456789]是手机号第二位可能出现的数字
     */
    val REGEX_MOBILE = "^[1][3456789][0-9]{9}$"

    /**
     * 校验手机号
     * @param mobile
     * @return 校验通过返回true，否则返回false
     */
    fun isMobile(mobile: String): Boolean {
        return Pattern.matches(REGEX_MOBILE, mobile)
    }

    fun isPrompt(msg: String): Boolean {
        return isEmpty(msg) || msg.contains("请选择")
    }

    fun isDateNull(str: String): Boolean {
        return isEmpty(str) || str == "-"
    }

}

